package manage.controller;

import manage.dao.UserMapper;
import manage.model.AccumulationFund;
import manage.model.User;
import manage.service.impl.AccumulationFundServiceImpl;
import manage.util.LogUtil;
import manage.util.Msg;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@Controller
public class AccumulationFundController {
    @Autowired
    private AccumulationFundServiceImpl accumulationFundService;

    @Autowired
    private UserMapper userMapper;

    private User user;
    //返回AccumulationFund的list
    @RequestMapping("/accumulationFund")
    @ResponseBody
    public Msg AccumulationFundData(HttpServletRequest request, Authentication authentication){
        String page = request.getParameter("currentPage");
        String limit = request.getParameter("pageSize");
        UserDetails userDetails =(UserDetails) authentication.getPrincipal();
        user = userMapper.selectByPrimaryKey(userDetails.getUsername());
        return Msg.success().add("data",accumulationFundService.selectAll(page,limit));
    }
    //添加AccumulationFund
    @ResponseBody
    @RequestMapping(value = "/addAccumulationFund",method = RequestMethod.POST)
    public Msg addAccumulationFund(AccumulationFund accumulationFund){

        String s= accumulationFundService.insert(accumulationFund);
        return Msg.attendanceAdd(s);
    }

    //修改AccumulationFund
    @ResponseBody
    @RequestMapping(value = "/addAccumulationFund",method = RequestMethod.PUT)
    public Msg updateAccumulationFund(AccumulationFund accumulationFund){

        String s= accumulationFundService.update(accumulationFund);
        return Msg.attendanceAdd(s);
    }
    //删除AccumulationFund
    @ResponseBody
    @RequestMapping("/delAccumulationFund")
    public Msg delAccumulationFund(@RequestBody Integer id){
        String s = accumulationFundService.del(id);
        LogUtil.writeFileLog(user.getAccount(),"删除文件类型:"+id.toString());
        return Msg.attendanceAdd(s);
    }

    //根据主键查AccumulationFund 和修改配合使用
    @ResponseBody
    @RequestMapping("/accumulationFundSelect")
    public Msg selAccumulationFund(@RequestBody Integer id){
        AccumulationFund accumulationFund = accumulationFundService.select(id);
        return Msg.success().add("accumulationFund",accumulationFund);
    }
    @ResponseBody
    @RequestMapping(value = "/importShortTel")
    public String InputExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
        String flag = "02";// 上传标志
        if (!file.isEmpty()) {
            try {
                String originalFilename = file.getOriginalFilename();// 原文件名字
                //log.info("文件名：" + originalFilename);
                InputStream is = file.getInputStream();// 获取输入流
                flag = accumulationFundService.InputExcel(is, originalFilename);
            } catch (Exception e) {
                flag = "03";// 上传出错
                e.printStackTrace();
            }
        }
        return flag;
    }
}
